var material_update = new Object();
material_update.businessIds = [];
material_update.business_zTree;

material_update.init = function () {
    material_update.getData();
    material_update.initBusinessTree();
    material_update.handle();
    material_update.deleteBus();
};

material_update.getData = function () {
    $("#business_select2").select2({
        tags: true,
        data: business_json,
        multiple: true,
        placeholder: "请选择行业",
        language: "zh-CN"
    })
};

//行业树
material_update.initBusinessTree = function () {
    // 加载树
    material_update.business_zTree = $.fn.zTree.init($("#group_tree"), {
        check: {
            enable: true,
            chkboxType: {"Y": "s", "N": "s"}
            //nocheckInherit: true
        },
        data: {
            simpleData: {
                enable: true
            }
        },
        view: {
            dblClickExpand: false
        },
        callback: {
            onCheck: function (event, treeId, treeNode) {
                var nodes = material_update.business_zTree.getCheckedNodes(true);
                var data = [];
                var businessName = [];
                var n;
                for (var i = 0; i < nodes.length; i++) {
                    var n = nodes[i];
                    if (n.pId == null) {
                        data = [];
                        data.push(n.id);
                        businessName = [];
                        businessName.push(n.name);
                        $("#business_select2").val(data).trigger("change");
                        if (nodes.length > 1) {
                            material_update.cancelNode(material_update.business_zTree, n);
                        }
                        $("#businessId").val(data.join(","));
                        $("#businessName").val(businessName.join(","));
                        return;
                    }
                    data.push(n.id);
                    businessName.push(n.name);
                }
                $("#businessId").val(data.join(","));
                $("#businessName").val(businessName.join(","));
                //前台显示并查询
                $("#business_select2").val(data).trigger("change");
            }
        }
    }, business_tree);
    var business_id = $("#businessId").val();
    if (business_id) {
        var businessIds = business_id.split(",");
        for (var i = 0; i < businessIds.length; i++) {
            var val = businessIds[i];
            var nodes = material_update.business_zTree.getNodesByParam("id", val, null);
            if (nodes.length > 0) {
                material_update.business_zTree.checkNode(nodes[0], true, true);
            }
        }
        material_update.business_zTree.refresh();
        $("#business_select2").val(businessIds).trigger("change");
    }
};


//点击小X触发，取消树中节点的勾选状态
material_update.deleteBus = function () {
    $(document).delegate('.select2-selection__choice__remove', 'click', function () {

        var id = $(this).parent().attr('id');
        var treeObj = $.fn.zTree.getZTreeObj("group_tree");
        var node = treeObj.getNodeByParam("id", id, null);
        node.checked = false;
        treeObj.updateNode(node);
        var checkedNodes = treeObj.getCheckedNodes(true);
        var businessName = [];
        material_update.businessIds = [];
        if (checkedNodes.length > 0) {
            var businessName = [];
            material_update.businessIds = [];
            if(id == '1'){  //全部行业
                treeObj.checkAllNodes(false);
            }else{
                for (var i = 0; i < checkedNodes.length; i++) {
                    var n = checkedNodes[i];
                    material_update.businessIds.push(n.id);
                    businessName.push(n.name);
                }
            }
        }
        $("#businessId").val(material_update.businessIds.join(","));
        $("#businessName").val(businessName.join(","));
    })
};


material_update.selectBusiness = function () {
    //得到最新的行业数据
    var businesses = $("#business_select2").select2("data");
    material_update.getNewDate(businesses);
    $('.por').addClass('show');

};

//得到最新的行业数据
material_update.getNewDate = function (data) {
    var businessData = [];
    for (var i = 0; i < data.length; i++) {
        businessData.push(data[i].id);
    }
    material_update.businessIds = businessData;
};

material_update.cancelNode = function (treeObj, node) {
    treeObj.checkAllNodes(false);
    treeObj.checkNode(node, true, true);
};

material_update.getAllchildrenNodesId = function (treeNode) {
    var array = [];
    array.push(treeNode.id);
    if (treeNode.isParent) {
        var childrenNodes = treeNode.children;
        if (childrenNodes) {
            for (var i = 0; i < childrenNodes.length; i++) {
                // array.push(childrenNodes[i].id);
                array = array.concat(material_update.getAllchildrenNodesId(childrenNodes[i]))
            }
        }
    }
    return array;
};

material_update.getAllchildrenNodesName = function (treeNode) {
    var array = [];
    array.push(treeNode.name);
    if (treeNode.isParent) {
        var childrenNodes = treeNode.children;
        if (childrenNodes) {
            for (var i = 0; i < childrenNodes.length; i++) {
                array = array.concat(material_update.getAllchildrenNodesName(childrenNodes[i]))
            }
        }
    }
    return array;
};

material_update.handle = function () {
    //返回
    $("#btn_back").click(function () {
        parent.layer.closeAll();
    });
    //修改
    $("#updateSave").click(function () {
        var businessId = $("#businessId").val();
        if(!businessId){
            layer.msg('请选择行业', {icon: 2});
            return;
        }
        var fileId = $("#varFid").val();
        var sourceFile = {};
        if(fileId){
            sourceFile.fileId = fileId;
            var fileName = $("#varFileName").val();
            sourceFile.fileName = fileName;
            var width = $("#w").val();
            sourceFile.width = width;
            var height = $("#h").val();
            sourceFile.height = height;
        }
        if (sourceFile.fileId) {
            sourceFile = JSON.stringify(sourceFile);
        } else {
            sourceFile = "";
        }
        var params = {
            "id": $("#id").val(),
            "businessId": businessId,
            "businessName": $("#businessName").val(),
            "sourceFile": sourceFile,
            "sourceDesc": $("#sourceDesc").val()
        };
        $.ajax({
            url: appPath + "/admin/hiddenMaterial/updateSave",
            type: 'post',
            data: params,
            success: function (data) {
                if ("10000" == data.code) {
                    var index = parent.layer.getFrameIndex(window.name);
                    parent.layer.msg('修改成功');
                    parent.layer.close(index);
                    parent.hiddenMaterial.initListTable();
                } else {
                    layer.alert('操作失敗', {icon: 2, skin: 'layer-ext-moon'});
                }
            }
        });
    })
};




